package com.controller.selling 
{
	import com.events.selling.QuotationEvent;
	import com.model.valueObjects.VoQuotation;
	import com.service.QuotationMaster;
	import com.view.Selling.com.QotProductListPopup;
	import com.view.Selling.com.QuotationForm;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class QuotationController extends AbstractController
	{
	
		[Inject]
		public var quotationDetails:QuotationMaster;
		
		public function QuotationController()
		{
			super();
		}
		
		[EventHandler(event="QuotationEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(quotationDetails.getAllquotations(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			if(model.currentMenu=='qotBtn')
				model.currentSellTableIndex=0;
			
			model.quotationList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="QuotationEvent.GET_QUOT_LIST")]
		public function getQuotList():void{
			sh.executeServiceCall(quotationDetails.quotationListPopup() ,getQuotListResult, handleFault );
			
		}
		private function getQuotListResult(event:ResultEvent):void
		{
			model.quotationListPopup =event.result as ArrayCollection;
		}
		
		[EventHandler(event="QuotationEvent.GET_QUOT_SUPPLIER")]
		public function getQuotSupplier():void{
			sh.executeServiceCall(quotationDetails.quotSupplierData() ,getQuotSupplierResult, handleFault );
			
		}
		private function getQuotSupplierResult(event:ResultEvent):void
		{
			model.quotationSupplierList = event.result as ArrayCollection;
		}
		
		[EventHandler(event="QuotationEvent.ADD", properties="quotationvo")]
		public function add(quotationvo:VoQuotation):void
		{
			sh.executeServiceCall(quotationDetails.addNewquotation(quotationvo), addResult, handleFault );	
		}
		
		private function addResult(event:ResultEvent):void
		{
			if(model.saveCloseBtn)model.currentSellFormsIndex=0;	
			Alert.show( "Quotation added" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			model.qutCode=event.result as String;
			getAll();
		}
		
		[EventHandler(event="QuotationEvent.UPDATE", properties="quotationvo")]
		public function update(quotationvo:VoQuotation):void
		{
			sh.executeServiceCall(quotationDetails.editquotation(quotationvo), editResult, handleFault );	
		}
		
		private function editResult(event:ResultEvent):void
		{
			getAll();
			model.currentSellFormsIndex=0;	
			Alert.show( "Quotation Updated" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="QuotationEvent.DELETE", properties="serialnumber")]
		public function deletef(serialnumber:Number):void
		{
			sh.executeServiceCall(quotationDetails.deletequotation(serialnumber),deleteResult, handleFault );	
		}		
		private function deleteResult(event:ResultEvent):void
		{
			model.currentSellFormsIndex=0;	
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show( "Quotation Deleted" );	
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}